package com.ruoyi.integration.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.integration.dto.GlobalZRangeRequestDTO;
import com.ruoyi.integration.dto.HeatMapRequestDTO;
import com.ruoyi.integration.dto.RGBFusionSlicing;
import com.ruoyi.integration.dto.ZFromLonLatRequestDTO;
import com.ruoyi.integration.service.MapRenderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mapRender")
@RequiredArgsConstructor
public class MapRenderController {

    private final MapRenderService mapRenderService;

    // 接口1: 获取生态图Z值取值范围以及自然断点分级法断点数据
    @GetMapping(value = "/getTifZRange")
    public AjaxResult getTifZRange(@RequestParam("minioBucket") String minioBucket,
                                   @RequestParam("minioObject") String minioObject,
                                   @RequestParam("jenksCount") Integer jenksCount) {
        return executeServiceCall(
                () -> mapRenderService.getRifZRange(minioBucket, minioObject, jenksCount),
                "获取生态图Z值取值范围以及自然断点分级法断点数据成功"
        );
    }

    // 接口2: 获取Z值范围及分类数据
    @GetMapping(value = "/getTifClassfi")
    public AjaxResult getTifClassfi(@RequestParam("minioBucket") String minioBucket,
                                    @RequestParam("minioObject") String minioObject) {
        return executeServiceCall(
                () -> mapRenderService.getTifClassfi(minioBucket, minioObject),
                "获取Z值范围及分类数据成功"
        );
    }

    // 接口3: 全局Z值范围及断点数据
    @PostMapping(value = "/getTifGlobalZRange")
    public AjaxResult getTifGlobalZRange(@RequestBody GlobalZRangeRequestDTO globalZRangeRequestDTO) {
       return executeServiceCall(
               () -> mapRenderService.getTifGlobalZRange(globalZRangeRequestDTO),
               "获取全局Z值范围及断点数据成功"
       );
    }

    // 接口4: 生成渲染图
    @PostMapping(value = "/tif2HeatMap")
    public AjaxResult tif2HeatMap(@RequestBody HeatMapRequestDTO heatMapRequestDTO) {
       return executeServiceCall(
               () -> mapRenderService.tif2HeatMap(heatMapRequestDTO),
               "生成渲染图成功"
       );
    }

    // 接口5: 根据经纬度获取Z值
    @PostMapping(value = "/getTifzData")
    public AjaxResult getTifZData(@RequestBody ZFromLonLatRequestDTO zFromLonLatRequestDTO) {
        return executeServiceCall(
                () -> mapRenderService.getTifZData(zFromLonLatRequestDTO),
                "根据经纬度获取Z值成功"
        );
    }


    // 接口6: 多光谱RGB融合切片
    @PostMapping(value = "/multisbandstif2png")
    public AjaxResult multisBandsTif2png(@RequestBody RGBFusionSlicing rgbFusionSlicing) {
        return executeServiceCall(
                () -> mapRenderService.multisBandsTif2png(rgbFusionSlicing),
                "多光谱RGB融合切片发布成功"
        );
    }

    // 统一响应处理
    private AjaxResult processServiceResponse(JSONObject response, String successMessage) {
        if (response != null) {
            // 检查服务返回是否包含错误标识
            if (response.containsKey("success") && !response.getBoolean("success")) {
                String errorMessage = response.getString("message");
                return AjaxResult.error(errorMessage != null ? errorMessage : "请求处理失败");
            }
            // 提取data字段作为实际数据
            if (response.containsKey("data")) {
                Object data = response.get("data");
                if (data instanceof JSONObject) {
                    return AjaxResult.success(successMessage, data);
                }
            }
            // 如果没有data字段，直接返回整个response
            return AjaxResult.success(successMessage, response);
        }
        return AjaxResult.error("请求处理失败");
    }

   // 统一服务调用包装
    private AjaxResult executeServiceCall(java.util.function.Supplier<JSONObject> serviceCall, String successMessage) {
        try {
            JSONObject response = serviceCall.get();
            return processServiceResponse(response, successMessage);
        } catch (Exception e) {
            return AjaxResult.error("服务调用异常: " + e.getMessage());
        }
    }

}